package com.pdf;

import java.io.*;

/**
 * @User xiangyl
 * @Data 2020/11/14
 */
public class IoReaderPdf {

    public static void main(String[] args) {
        FileInputStream in=null;
        FileOutputStream out=null;
        try {
            in = new FileInputStream("D:\\test\\storage.pdf");
            out = new FileOutputStream("D:\\test\\pdfContent.txt");
            byte buf[] = new byte[2*1024];
            int bytesRead = in.read(buf);
            while (bytesRead >= 0) {
                out.write(buf, 0, bytesRead);
                bytesRead = in.read(buf);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                out.flush();
                out.close();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /**hadoop中流的互拷，源码*/
    public static void copyBytes(InputStream in, OutputStream out, int buffSize)
            throws IOException {
        PrintStream ps = out instanceof PrintStream ? (PrintStream)out : null;
        byte buf[] = new byte[buffSize];
        int bytesRead = in.read(buf);
        while (bytesRead >= 0) {
            out.write(buf, 0, bytesRead);
            if ((ps != null) && ps.checkError()) {
                throw new IOException("Unable to write to output stream.");
            }
            bytesRead = in.read(buf);
        }
    }

}
